<template>
  <div>
    <table border="1" width="700" style="border-collapse: collapse">
      <caption>
        购物车
      </caption>
      <thead>
        <good-thead :list="goodList" @isAllche="isAllcheche" />
      </thead>
      <good-tbody
        v-for="(item, index) in goodList"
        :key="index"
        :name="item.name"
        :price="item.price"
        :num="item.num"
        :checked="item.checked"
        @BtnSub="CountSub"
        @BtnAdd="CountAdd"
        @BtnDel="Del"
        @check="CheckALL"
      />

      <tfoot>
        <tr>
          <td>合计:</td>
          <td colspan="5"></td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
import GoodTbody from './components/GoodTbody.vue'
import GoodThead from './components/GoodThead.vue'
export default {
  components: { GoodTbody, GoodThead },
  data () {
    return {
      goodList: [
        {
          name: '诸葛亮',
          price: 1000,
          num: 1,
          checked: false
        },
        {
          name: '蔡文姬',
          price: 1500,
          num: 1,
          checked: false
        },
        {
          name: '妲己',
          price: 2000,
          num: 1,
          checked: false
        },
        {
          name: '鲁班',
          price: 2200,
          num: 1,
          checked: false
        }
      ]
    }
  },
  methods: {
    CountSub (e) {
      // return e--
      const index = this.goodList.findIndex((item) => item.name === e)
      if (this.goodList[index].num === 0) {
        return alert('不能再减咯！')
      }
      this.goodList[index].num--
    },
    CountAdd (e) {
      const index = this.goodList.findIndex((item) => item.name === e)
      this.goodList[index].num++
    },
    Del (e) {
      const index = this.goodList.findIndex((item) => item.name === e)
      this.goodList.splice(index, 1)
    },

    CheckALL (e) {
      const index = this.goodList.findIndex((item) => item.name === e[1])
      return (this.goodList[index].checked = e[0])
    },
    isAllcheche (val) {
      console.log(val)
      return this.goodList.forEach((item) => (item.checked = val))
    }
  }
}
</script>

<style></style>
